﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace vpn
{
    public partial class Form1 : Form
    {
        private static string vpnname = "MyVPN";
        public Form1()
        {
            InitializeComponent();
        }

        private void bt_Connect_Click(object sender, EventArgs e)
        {
            try
            {
                if (tb_Username.Text.Trim().Equals(Consts.DefaultUser))
                {
                    if (vpn.CountBomb.GetLastTimes() < 1)
                    {
                        ads adsDlg = new ads();
                        adsDlg.ShowDialog();
                        return;
                    }
                }

                if (!ValidateParams())
                    return;

                VPNHelper vpnhelper = new VPNHelper(tb_ServerName.Text.Trim(), vpnname, tb_Username.Text.Trim(), tb_password.Text.Trim());
                vpnhelper.CreateOrUpdateVPN();
                vpnhelper.TryConnectVPN();
                bt_Connect.Enabled = false;
                bt_disconnect.Enabled = true;
                System.Threading.Thread.Sleep(5000);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private bool ValidateParams()
        {
            string severname = tb_ServerName.Text.Trim();
            if (severname != "www.vpnsrvs.com"
                && severname != "vpnsrvs.com"
                && severname != "50.18.143.47"
                && severname != "204.236.182.160")
            {
                MessageBox.Show("Please input correct server name");
                return false;
            }

            if (string.IsNullOrEmpty(tb_Username.Text.Trim()) || string.IsNullOrEmpty(tb_password.Text.Trim()))
                return false;

            return true;
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            vpn.CountBomb.Validate();

            //init default user name and password
            tb_ServerName.Text      =   Consts.DefaultServer;
            tb_Username.Text        =   Consts.DefaultUser;
            tb_password.Text        =   Consts.DefaultUserPass;

            int lasttimes = vpn.CountBomb.GetLastTimes();

            if (vpn.CountBomb.GetLastTimes() < 1)
            {
                lab_lastTimes.Text = "0";
                //controlPanel.Enabled = false;
                tb_Username.Clear();
                tb_password.Clear();
            }
            else
            {
                lab_lastTimes.Text = lasttimes.ToString();
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start(vpn.Consts.VPN_website);
        }

        private void bt_disconnect_Click(object sender, EventArgs e)
        {
            try
            {
                bt_disconnect.Enabled = false;
                
                VPNHelper vpn = new VPNHelper();
                vpn.TryDisConnectVPN(vpnname);
                System.Threading.Thread.Sleep(3000);
                //vpn.TryDeleteVPN(vpnname);

                bt_Connect.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btn_Buy_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(Consts.TaobaoLink);
        }
    }
}
